package bitcat.chuenyee.utils;

import cn.hutool.core.io.FileUtil;
import cn.hutool.core.io.watch.WatchMonitor;
import cn.hutool.core.io.watch.Watcher;
import cn.hutool.extra.ftp.SimpleFtpServer;
import org.apache.ftpserver.usermanager.impl.BaseUser;

import java.io.File;

/**
 * <p>
 *
 * </p>
 *
 * @author chuenyee
 * @since 2021/10/22 11:32
 */
public class FileUntil {

    /**
     * 本地监听
     * */
    public void listener(String path,Watcher watcher,int maxDepth){
        File file = FileUtil.file(path);
        WatchMonitor watchMonitor = WatchMonitor.createAll(file,watcher);

//设置监听目录的最大深入，目录层级大于制定层级的变更将不被监听，默认只监听当前层级目录
        watchMonitor.setMaxDepth(maxDepth);
//启动监听
        watchMonitor.start();
    }

    /**
     * 开启匿名ftp
     * */
    public static void ftp(String path){
        SimpleFtpServer
                .create()
                // 此目录必须存在
                .addAnonymous(path)
                .start();
    }

    /**
     * 开启自定义用户FTP
     * */
    public static void ftp(String path,String username,String password){
        BaseUser user = new BaseUser();
        user.setName(username);
        user.setPassword(password);
        user.setHomeDirectory(path);

        SimpleFtpServer
                .create()
                .addUser(user)
                .start();
    }

}
